package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.OccupationEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.OccupationService;


@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebOccupationController {

    @Resource(name="occupationService")
    private OccupationService occupationService;
    
    /**
     * 跳转职业到首页
     * @return
     */
    @RequestMapping(value = "/toOccupationIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toEducationIndex() {
        ModelAndView mav = new ModelAndView("/sec/sec_zhiye");
        
        return mav;
    }
    
    /**
     * 职业数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllOccupation", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllOccupation(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = occupationService.getListOccupationCount(name);
        
        //根据条件查询当前页数据
        List<OccupationEntity> list = occupationService.getAllOccupation(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<OccupationEntity> page = new Page<OccupationEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<OccupationEntity> pageModel = new PageModelList<OccupationEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 职业添加
     */
    @RequestMapping(value = "/toSaveOccupation")
    @ResponseBody
    public int toSaveOccupation(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        
        OccupationEntity occupationEntity = new OccupationEntity();
        
        occupationEntity.setName(name);
        occupationEntity.setPrice(price);
        
        int flag = occupationService.saveOccupation(occupationEntity);
        
        return flag;
    }
    
    /**
     * 职业删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteOccupationId")
    @ResponseBody
    public int toDeleteOccupationId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = occupationService.deleteOccupationId(id);
        return flag;
    }
    
    /**
     * 职业修改
     */
    
    @RequestMapping(value = "/toUpdateOccupationId")
    @ResponseBody
    public int toUpdateOccupationId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        double price = Double.parseDouble(request.getParameter("price"));
        
        OccupationEntity occupationEntity = new OccupationEntity();
        occupationEntity.setName(name);
        occupationEntity.setPrice(price);
        occupationEntity.setId(id);
        
        int flag = occupationService.updateOccupationId(occupationEntity);
        return flag;
    }
    
}
